<?php
namespace app\admin\controller;

use think\Controller;

class Configs extends  Controller
{

    public function edit(){
        $id=input('id');
        if(request()->isAjax()){
           $users=input('post.');
           $update=db('configs')->where('id',$users['id'])->update($users);
           if ($update){
               $res=['code'=>200,'msg'=>"更新数据成功!"];
           }else{
               $res=['code'=>1,'msg'=>"更新数据失败!"];
           }
           return json($res);
        }
        $data=db('configs')->find($id);
        $this->assign('data', $data);
        return view('edit');
    }
    public function main(){
        if(\request()->isAjax()){
            $map=array();
            $username=input('username');
            $sex=input('sex');
            $classify=input('classify');
            $limit=input('limit');
            $limit=$limit?$limit:10;
            $page=input('page');
            if($username){
                $map['username']=$username;
            }
            if($sex){
                $map['sex']=$sex;
            }
            if($classify){
                $map['classify']=$classify;
            }
            $data=db('configs')->where($map)->limit($limit)->page($page)->select();
            $count=db('configs')->where($map)->count();
            $res["msg"] = "";
            $res["code"] = 0;
            $res["count"] = $count;
            $res["data"] = $data;
            if(empty($data)){
                $res["msg"]="暂无数据";
            }
            return json($res);
        }
        return view('main');
    }
    public function getSexAttr($data){
        foreach ($data as $k => $v) {
            $v['sex']=$this->getSexAttr($v['sex']);
            $data[$k]['sex']=$v['sex'];
        }
        //$this->getSexAttr($data['sex']);
        dump($data);
        die();
    }
    public function add(){
        if(request()->isAjax()){
           $data=input('post.');
           $add=db('configs')->insert($data);
           if($add){
               // return json(array('code' => 200, 'msg' => '添加成功'));
               $res=[
                   'code'=>200,
                   'msg' => '添加成功'];
               return $res;
           }else{
               return json(array('code' => 0, 'msg' => '添加失败'));
           }
        }
    return view();
    }
    public function  del(){
        $id=input('id');
        $del=db('configs')->delete($id);
        if($del){
            $res=['status'=>200,'msg'=>"删除数据成功!"];
        }else{
            $res=['status'=>1,'msg'=>"删除数据失败!"];
        }

        return json($res);
    }

}
